﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;
using System.Data.OracleClient;

namespace TierApplication
{
    public partial class T_MAILSENDREC
    {
        public bool SendEMail(T_MAILSENDREC ent,ref string result)
        {
            string id = GetMailSendId();
            if (id == "-1") return false;
            try
            {
                ent.MAILSENDID = id;
                ent.Add();
                int ai_result = -1;
                string ac_result = string.Empty;
                SendMail(id, ref ai_result, ref ac_result);
                if (ai_result < 0)
                {
                    return false;
                }
                result = ac_result;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string GetMailSendId()
        {
            string sql = "Select get_mailsendid() From dual";
            DataSet ds = DbHelperOra.ExecuteDataSet(sql);
            if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            else
            {
                return "-1";
            }
        }

        public void SendMail(string MailSendId, ref int ai_result, ref string ac_result)
        {
            OracleParameter[] parameters =  {
                new OracleParameter("AC_MAILSENDID",OracleType.VarChar,50),
                new OracleParameter("AI_RESULT",OracleType.VarChar,100),
                new OracleParameter("AC_RESULT",OracleType.VarChar,50)
                                            };
            parameters[0].Value = MailSendId;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[1].Direction = ParameterDirection.Output;
            parameters[2].Direction = ParameterDirection.Output;
            try
            {
                OracleCommand cmd = DbHelperOra.RunProcedureReturnCammand("SP_SEND_EMAIL_BY_MAILSENDREC", parameters);
                ai_result = int.Parse(cmd.Parameters["AI_RESULT"].Value.ToString());
                ac_result = cmd.Parameters["AC_RESULT"].Value.ToString();

            }
            catch (Exception e)
            {
                ai_result = -1;
                ac_result = "调用后台过程错误,错误原因为：" + TierService.PageValidate.RemoveChr10And13(e.Message);
            }
        }
    }
}
